import { Text, useMessage } from 'alemonjs'
import { selects, validate } from '@src/apps/index'
import { user_attribute } from '@src/db/src/models/user/user_attribute'
import { qidToUid } from '@src/apps/tools/qid_goto_uid'

export const regular = /^(!|！|\/)?(属性|面板)$/

const response = onResponse(selects, async e => {
  const [message] = useMessage(e)
  const userId = await qidToUid(e.UserId ?? e.UserKey)

  // 读取用户属性
  const attr = await user_attribute.findOneValue({ where: { uid: userId } })
  if (!attr) {
    message.send(format(Text('\n暂无属性！')))
    return
  }

  // 格式化输出
  let content = '\n===你的属性===\n'
  const attrMap: Record<string, string> = {
    level: '等级',
    stage: '等阶',
    exp: '经验',
    constitution_final: '体质',
    spirit_final: '精神',
    strength_final: '力量',
    intelligence_final: '智力',
    agility_final: '敏捷',
    perception_final: '感知',
    charm_final: '魅力',
    luck_final: '幸运',
    hp_max: '生命上限',
    hp: '当前生命',
    mp_max: '魔力上限',
    mp: '当前魔力',
    atk: '物理攻击力',
    matk: '魔法攻击力',
    def: '物理防御力',
    mdef: '魔法防御力',
    atk_mastery: '物理精通',
    matk_mastery: '魔法精通',
    atk_resist: '物理抗性',
    matk_resist: '魔法抗性',
    hit: '命中',
    dodge: '闪避',
    crit: '暴击',
    crit_dmg: '暴伤',
    crit_resist: '暴免',
    crit_avoid: '暴抗',
    speed: '速度',
    tenacity: '韧性',
    hp_recover: '生命恢复',
    mp_recover: '魔力恢复'
  }

  for (const [key, value] of Object.entries(attr)) {
    if (attrMap[key]) {
      content += `${attrMap[key]}: ${value}\n`
    }
  }

  message.send(format(Text(content)))
})

export default onResponse(selects, [validate, response.current])
